﻿using MediatR;
using Newtonsoft.Json;
using System.Collections.Generic;
using W.Common;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.AllocationCommand;
using W.Warehouse.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.AllocationCommandHandler
{
    public class ProductStorageProductListCommandHandler:IRequestHandler<ProductStorageProductListCommand, PageDTO<AllocationProductRuAWarehouseDTO>>
    {
        private readonly IStorageProductsRepository _istorageProductsRepository;
        public ProductStorageProductListCommandHandler(IStorageProductsRepository istorageProductsRepository)
        {
            _istorageProductsRepository = istorageProductsRepository;
        }

        public async Task<PageDTO<AllocationProductRuAWarehouseDTO>> Handle(ProductStorageProductListCommand request, CancellationToken cancellationToken)
        {
            List<ProductInfo> productInfos = await GetStorageProducts(1, 100);
            PageDTO<AllocationProductRuAWarehouseDTO> page = new PageDTO<AllocationProductRuAWarehouseDTO>();
            var list = (from a in await _istorageProductsRepository.GetAllAsync()
                       join b in productInfos on a.ProductNo equals b.ProductNo
                       where a.IsDelete==false && a.ProductNo == b.ProductNo 
                       select new AllocationProductRuAWarehouseDTO
                       {
                           ProductId=b.ProductId,
                           ProductName = b.ProductName,
                           ProductNo = b.ProductNo,
                           ProductSpec = b.ProductSpec,
                           ProductBatch = a.Batch,
                           StatisticsBatch = a.Batch,
                           ProductPrice = a.Price,
                           BinLocationName = a.BinLocationName,
                           UnitId = b.UnitName,
                           ProductWrap=b.ProductWrap,
                           ProductWeight=b.ProductWeight,
                       }
                ).DistinctBy(c=>c.ProductNo).ToList();
            if (!string.IsNullOrEmpty(request.ProductNo))
            {
                list = list.Where(x => x.ProductNo == request.ProductNo).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductName))
            {
                list = list.Where(x => x.ProductName.Contains(request.ProductName)).ToList();
            }
            page.TotalCount = list.Count();
            page.TotalPage = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = list.OrderByDescending(c => c.ProductId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
        /// <summary>
        /// 产品
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetStorageProducts(int pageIndex, int pageSize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI0Iiwic2lkIjoiMTg1OTgwNTA4MDc2NTczMDgxNiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvZ3JvdXBzaWQiOiIiLCJqdGkiOiI5MWU1MzVhMy1kZjVlLTQxNGQtODdkMy0zYjE3MmIzNmRhZWEiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LqO5a2Q5ra1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.9e371kkhbGOGIALMM210Z0Mhka1BhADJFTKFd8w3AJY";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string storage = "http://10.223.8.18:8000/productRead/api/ProductSelect/GetProductInfos?index=" + pageIndex + "&size=" + pageSize;

            string postRepsonse = await cilent.GetAsync(storage);
            var json = JsonConvert.DeserializeObject<ApiResultList<ProductInfo>>(postRepsonse);
            return json.data.list;
        }

        /// <summary>
        /// 产品管理
        /// </summary>
        public class ProductInfo
        {
            /// <summary>
            /// 产品id
            /// </summary>
            public int ProductId { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductNo { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductName { get; set; }
            /// <summary>
            /// 厂家编码
            /// </summary>
            public string? ManuNo { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InsideNo { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            public int? UnitId { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            public string? UnitName { get; set; }
            /// <summary>
            /// 类别
            /// </summary>
            public int? ProductTypeId { get; set; }
            /// <summary>
            /// 上线值
            /// </summary>
            public int? GoOnline { get; set; }
            /// <summary>
            /// 下限值
            /// </summary>
            public int? Online { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? ProductSpec { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? ProductPrice { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 默认库位
            /// </summary>
            public int? BinLocationId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public string? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public string? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? ProductDesc { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public string? ProductWrap { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public string? ProductWeight { get; set; }
            /// <summary>
            /// 公司Id
            /// </summary>
            public string? officeId { get; set; }
            /// <summary>
            /// 用户Id
            /// </summary>
            public int? userid { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool? IsDelete { get; set; }
        }
    }
}
